package com.imau.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imau.common.R;
import com.imau.common.ResultCodeEnum;
import com.imau.entity.Course;
import com.imau.service.CourseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(tags = "课程管理")
@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @ApiOperation("添加课程信息")
    @PostMapping
    public R save(@RequestBody Course course) {

        courseService.save(course);

        return R.success(ResultCodeEnum.SUCCESS, "保存课程信息成功");
    }

    @ApiOperation("删除课程信息")
    @DeleteMapping("/delete/{id}")
    public R deleteById(@PathVariable Long id) {

        courseService.removeById(id);

        return R.success(ResultCodeEnum.SUCCESS, "删除课程信息成功");
    }

    @ApiOperation("修改课程信息")
    @PutMapping
    public R update(@RequestBody Course course) {

        courseService.updateById(course);

        return R.success(ResultCodeEnum.SUCCESS, "修改学生信息成功");
    }

    @ApiOperation("分页查询课程信息||条件查询")
    @GetMapping("/information")
    public R<Page> page(int page, int pageSize) {
        log.info("参数信息：page:{},pageSize:{},name:{}", page, pageSize);

        // 分页构造器
        Page<Course> pageInfo = new Page<>(page, pageSize);

        // 条件构造器
        LambdaQueryWrapper<Course> queryWrapper = new LambdaQueryWrapper<>();

        // 分页查询
        courseService.page(pageInfo, queryWrapper);

        return R.success(pageInfo, ResultCodeEnum.SUCCESS);

    }

}
